/*
	Copyright (c) 2004-2009, The Dojo Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/


if(!dojo._hasResource["icubox.AppTest"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["icubox.AppTest"] = true;
dojo.provide("icubox.AppTest");

//Require some common widget resources
dojo.require("dijit._Widget");
dojo.require("dijit._Templated");
dojo.require("icubox.LivePlayer");

dojo.declare("icubox.AppTest",
    [dijit._Widget, dijit._Templated], {        		    	    
        templateString: "<div class=\"score\"><br/>SCORE:<span dojoAttachPoint='operation'></span></div>",
        timer:"",
        point:10,
        score:0,
        correctAnswer:0,
        lives:"",
        postCreate : function() { 
         this.operation.innerHTML = "0"; 
         dojo.create("div",{ id:"player",style:"visibility:hidden" },"centerpanel","first");
         dojo.create("div",{ id:"question",style:"visibility:hidden" },"centerpanel");                
         dojo.create("div",{ id:"ball1",class:"ballblue",style:"visibility:hidden" },"centerpanel");                
         dojo.create("div",{ id:"ball2",class:"ballblue",style:"visibility:hidden" },"centerpanel");                
         dojo.create("div",{ id:"ball3",class:"ballblue",style:"visibility:hidden" },"centerpanel");
         dojo.create("div",{ id:"ball1",class:"ballblue",style:"visibility:hidden" },"centerpanel");                                
         dojo.create("div",{ id:"start",style:"visibility:visible"  },"centerpanel"); 
         
         this.lives = new icubox.LivePlayer({ id: "lpw"});
         this.lives.placeAt("centerpanel");
         
         dojo.connect(dojo.byId("start"), "onclick", dojo.hitch(this, "_beginGame"));
         dojo.connect(document, "onkeypress", dojo.hitch(this, "_handlerKey"));
       },
       _initApp :function(){
         // Generar un boton para iniciar el inicio del Game
         dojo.style("start","visibility","visible");
       },            
       _beginGame : function(){                         
         // start Game
         dojo.style("start","visibility","hidden");   
         dojo.style("player","visibility","visible");
         dojo.style("lpw","visibility","visible");                                                                                      
         // muestra la question 
         dojo.attr("question","innerHTML","Cuanto es ???"); 
         // muestra las vidas 
         this.lives.resetLive();
         this._initBall();    
       },
       _initBall: function(){
         // move ball
         dojo.style("ball1","left","10%");                                           
         dojo.style("ball2","left","50%");                                           
         dojo.style("ball3","left","90%");                                           
         dojo.style("ball1","top","5%");                                           
         dojo.style("ball2","top","5%");                                           
         dojo.style("ball3","top","5%");                                           
         
         // realizar un funcion para generar movimiento de las pelotitas 
         dojo.style("ball1","visibility","visible");                                           
         dojo.style("ball2","visibility","visible");                                           
         dojo.style("ball3","visibility","visible");                                                    
         dojo.style("question","visibility","visible");
         this._putProblem();
         this._updateBall();
       },
       _updateBall: function(){
         var top = dojo.style("ball1", "top");
         var left_player = dojo.style("player", "left");                    
               if(top < 340  &&  top > 320  ){ 
                  // simple collision detection
                  //  130 - 145 - first apple 
                  //  385 - 415 - midle apple
                  //  640 - 670 - last apple
                  if( left_player >= 130 && left_player <= 145 ){
                      this._pointGame(1); 
                      return;                        
                  }
                  if( left_player >= 385 && left_player <= 415 ){
                      this._pointGame(2);                     
                      return;
                  }
                  if( left_player >= 640 && left_player <= 670 ){                                       
                      this._pointGame(3);                     
                      return;
                  }                                   
               } // end if
               if(top > 340 ){                 
                  dojo.style("ball1","visibility","hidden");                                           
                  dojo.style("ball2","visibility","hidden");                                           
                  dojo.style("ball3","visibility","hidden");                                                                       
                  this._deleteLive();
                  return;
               }
               dojo.style("ball1","top",top+10);                                             
               dojo.style("ball2","top",top+10);  
               dojo.style("ball3","top",top+10);                           
               timer = setTimeout(dojo.hitch(this, "_updateBall"),200);                                            
       },
       _newGame : function(){                         
         // start Game                 
       },
       _endGame : function(){                         
         // finish Game
        // dojo.unconnect(document, "onkeypress" );
       },
       _pointGame : function(answer){                         
         if(this.correctAnswer == answer){
           this.score = parseInt(this.score)+parseInt(this.point); 
           this.operation.innerHTML= this.score; 
           this._initBall();
         }else{
             this._deleteLive();
             return;          
         } // end if            
       },
       _putProblem : function(){
          var _ball1,_ball2,_ball3;
          var a = Math.floor(11*Math.random());
          var b = Math.floor(11*Math.random());
          var result = a+b;
          var _p = a+" + "+b; 

         dojo.attr("question","innerHTML","Cuanto es :"+_p); 

         // Se debe seleccionar el apple donde ira el valor ganador 
         this.correctAnswer = Math.ceil(3*Math.random());
           if( this.correctAnswer       == 1 ){
              _ball1 = "ball1";
              _ball2 = "ball2";
              _ball3 = "ball3";
           }else if( this.correctAnswer == 2 ){
              _ball1 = "ball2";
              _ball2 = "ball1";
              _ball3 = "ball3";
           }else if( this.correctAnswer == 3 ){
              _ball1 = "ball3";
              _ball2 = "ball1";
              _ball3 = "ball2";
           }                       
         dojo.attr(dojo.byId(_ball1),"innerHTML",result);  
         dojo.attr(dojo.byId(_ball2),"innerHTML",result  + Math.ceil(10*Math.random())  );  
         dojo.attr(dojo.byId(_ball3),"innerHTML",result - Math.ceil(10*Math.random()) );  
       },
       _deleteLive : function() {
              this.lives.deleteLive();          
              // Cuando se determina la ultima vida mostrar imagen GAME OVER 
              if (this.lives.getLive() == 0 ){
                  dojo.style("start","backgroundImage","url('./img/gameover.png')");
                  dojo.style("start","visibility","visible"); 
              return;                
              }    
              this._initBall();        
       },
       _handlerKey : function(evt){
          // determinar limite left & right para el game               
              if (evt.keyCode == dojo.keys.LEFT_ARROW){
                 var left = dojo.style("player", "left")-15;
                     dojo.style("player", "left", left);
              }else if(evt.keyCode == dojo.keys.RIGHT_ARROW){
                 var left = dojo.style("player", "left")+15;
                     dojo.style("player", "left", left);
             }
       }
      
   } // end icubox.AppTest
);

}
